package com.xry.test1.controller;

import com.xry.test1.model.LightControlRequest;
import com.xry.test1.model.LightData;
import com.xry.test1.service.impl.FanServiceImpl;
import com.xry.test1.service.impl.LightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/fan")
public class FanController {

    @Autowired
    private FanServiceImpl fanService;
    /**
     * 获取灯的当前状态
     */
//    @GetMapping("/state")
//    public LightData getLightState() {
//        return lightService.getLightState();
//    }

        //仅可手动风扇控制
//    public static class FanRequest {
//        private Integer gear;
//
//        public Integer getGear() {
//            return gear;
//        }
//
//        public void setGear(Integer gear) {
//            this.gear = gear;
//        }
//    }
//
//    @PostMapping("/control")
//    public ResponseEntity<String> setFanSpeed(@RequestBody FanRequest request) {
//        Integer level = request.getGear();
//        System.out.println("收到挡位：" + level);
//        boolean success = fanService.controlFan(level);
//        return success ? ResponseEntity.ok("OK") : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("ERROR");
//    }

    public static class FanRequest {
        private String mode;
        private Integer gear;

        public String getMode() {
            return mode;
        }

        public void setMode(String mode) {
            this.mode = mode;
        }

        public Integer getGear() {
            return gear;
        }

        public void setGear(Integer gear) {
            this.gear = gear;
        }
    }

    @PostMapping("/control")
    public ResponseEntity<String> setFanSpeed(@RequestBody FanRequest request) {
        String mode = request.getMode();
        Integer level = request.getGear();

        System.out.println("收到模式：" + mode + "，挡位：" + level);

        boolean success;
        if ("auto".equalsIgnoreCase(mode)) {
            success = fanService.setAutoMode(); // 后端自动调节
        } else {
            success = fanService.controlFan(level); // 手动设置
        }

        return success
                ? ResponseEntity.ok("OK")
                : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("ERROR");
    }

}
